#!/usr/bin/perl

#NOTE: IN ORDER TO USE THIS SCRIPT, YOU MUST FIRST QUERY A SET OF ALL BIB RECORDS

#THEN YOU MUST "EXPORT" INFORMATION FROM CATALOG
#for instance:

#   RECORD #(BIBLIO)

#THE RESULTING TEXT FILE NEEDS TO BE LOADED INTO ./authorities_deleted/

use Cwd;

$baseurl = getcwd;

if ($ARGV[0] eq 'CREATING') {
    $mode = 'CREATING';
} else {
    $mode = 'UPDATING';
}

if ($ARGV[1] =~ /\//) {
    ($baseurl = $ARGV[1]) =~ s/[\/]*$//;
}

$error_outs = $baseurl . '/errors_authorities_deleted.txt';
$print_outs = $baseurl . '/prints_authorities_deleted.txt';

binmode(STDOUT, ':utf8');

open (STDERR, ">$error_outs");
open (STDOUT, ">$print_outs");

print "baseurl: " . $baseurl . "\n";

# local system variables
$connect = $baseurl . '/connect.pl';
require $connect;

use IO::File;

&hashes_and_arrays; #SUBROUTINE AT END OF SCRIPT

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$start_time = "$HOUR:$MIN:$SEC";

$where = $baseurl . '/authorities_deleted';

##START: GET BIB NUMBERS OF DELETED RECORDS

use DBI;

# connection for the select
$dbh = DBI->connect("DBI:mysql:database=$db_name;host=$db_host", $db_user, $db_pass, { mysql_enable_utf8 =>1 }) or die "couldn't connect to database: " . DBI->errstr;

$this_table = $db_local_tables_hash{'authorities_record_table'};

$first_file_name = $where . '/undeletedauthorities1000000.out';
if(-e $first_file_name) {

        $querystring = "SELECT substr( local_control_number, 6 )
                        FROM $this_table
                        WHERE auth_status = 'x'";

        $query = $dbh->prepare($querystring)
            or die("couldn't prepare statement: ". $dbh->errstr);

        $query->execute()
            or die("couldn't execute statement: ". $dbh->errstr);

        while (@row = $query->fetchrow_array) {
            $already_deleted{$row[0]} = "$row[0]";
        }
}

@current_time = localtime;

##END: GET BIB NUMBERS OF DELETED RECORDS

###START: UPDATE $this_table TABLE LINES

while (defined($next_file = <$where/*.out>)) {

print $next_file . "\n";

    open(IN, "< $next_file") || die "cannot open $next_file for reading: $!";

($startoffile = $next_file) =~ s/$where\/undeletedauthorities//;
$startoffile =~ s/\.out//;
$next_auth_record = $startoffile;
$endoffile = $next_auth_record + $filename;

    while(<IN>) {

        if($_ !~ /^RECORD #/) {

        #THIS STARTS WHERE THE DELETED BIB RECORDS ARE LOADED
        $this_auth_record = substr($_, 1, 7);

            while($next_auth_record < $this_auth_record) {

                if($already_deleted{$next_auth_record}) {
                    delete($already_deleted{$next_auth_record});
                } else {
                    $update_auth_record = $institution . 'a' . $next_auth_record;
                    $querystring = "UPDATE $this_table
                                    set auth_status = 'x'
                                    WHERE local_control_number = ?";

                    $query = $dbh->prepare($querystring)
                        or die("couldn't prepare statement: ". $dbh->errstr);
    
                    $query->execute($update_auth_record)
                        or die("couldn't execute statement: ". $dbh->errstr);
                }

                $next_auth_record++;

            }

        #THIS ENDS WHERE THE DELETED BIB RECORDS ARE LOADED

        } #END OF if($_ !~ /^RECORD #/)

        $next_auth_record++;

        unlink($next_file);
    }

    close(IN) || "cannot close $next_file: $!";

            while ($next_auth_record < $endoffile) {

                if($already_deleted{$next_auth_record}) {
                    delete($already_deleted{$next_auth_record});
                } else {
                $update_auth_record = $institution . 'a' . $next_auth_record;
                $querystring = "UPDATE $this_table
                                set auth_status = 'x'
                                WHERE local_control_number = ?";

                $query = $dbh->prepare($querystring)
                    or die("couldn't prepare statement: ". $dbh->errstr);

                $query->execute($update_auth_record)
                    or die("couldn't execute statement: ". $dbh->errstr);

                $query->finish();

                }
    
                $next_auth_record++;

            }

}

###END: UPDATE $this_table TABLE LINES

####START: CREATING FILES FOR LOAD INTO CONSORTIUM AUTHORITIES TABLES

if ($consortium ne '') {

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$start_external_update_time = "$HOUR:$MIN:$SEC";

$last_update_time = ($current_time[5] + 1900) . "-" . ($current_time[4] + 1) . "-$current_time[3] $current_time[2]:$current_time[1]:$current_time[0]";

print "last_update_time: " . $last_update_time . "\n";

$this_table = $db_local_tables_hash{'authorities_record_table'};
$recordcount = 0;

    $querystring = "SHOW FIELDS FROM $this_table";

        $query = $dbh->prepare($querystring)
            or die("couldn't prepare statement: ". $dbh->errstr);

        $query->execute()
            or die("couldn't execute statement: ". $dbh->errstr);

    while ( @row = $query->fetchrow_array ) {
        push (@fields_array, $row[0]); ## field name
    }

        $query->finish();

pop(@fields_array);

    $fields_array_string = join(", ", @fields_array);

$consortium_authorities_keys_deleted = $baseurl . '/consortium_authorities_deleted/consortiumauthorities.key';

$consortium_authorities_keys_loader = IO::File->new("> $consortium_authorities_keys_deleted") || die "cannot open $consortium_authorities_keys_deleted for writing: $!\n";
binmode( $consortium_authorities_keys_loader, ':utf8' );
flock($consortium_authorities_keys_loader, 2);

    print $consortium_authorities_keys_loader $fields_array_string . "\n";

        $consortium_authorities_keys_loader->autoflush(1);
        close($consortium_authorities_keys_loader, 2) || "cannot close $consortium_authorities_keys_loader: $!";

$limit_start = 0;
$rownumber = 1;
while($rownumber > 0) {

        $querystring = "SELECT * from $this_table
            WHERE $this_table.solr_update >= ?
            AND auth_status = 'x'
            LIMIT ?, 10000";

        $query = $dbh->prepare($querystring)
            or die("couldn't prepare statement: ". $dbh->errstr);

        $query->execute($last_update_time, $limit_start)
            or die("couldn't execute statement: ". $dbh->errstr);

$limit_start += 10000;
$rownumber = $query->rows();

    print "rownumber: " . $rownumber . "\n";

while(@query_array = $query->fetchrow_array()) {
    $int = $recordcount / 10000;

    if ($int =~ m/^\d+$/) {

        if($recordcount > 0) {

        $consortium_authorities_batch_loader->autoflush(1);
        close($consortium_authorities_batch_loader, 2) || "cannot close $consortium_authorities_batch_loader: $!";

        }

$consortium_authorities_batch_deleted = $baseurl . '/consortium_authorities_deleted/consortium_authorities_deleted' . $recordcount .  '.txt';

$consortium_authorities_batch_loader = IO::File->new("> $consortium_authorities_batch_deleted") || die "cannot open $consortium_authorities_batch_deleted for writing: $!\n";
binmode( $consortium_authorities_batch_loader, ':utf8' );
flock($consortium_authorities_batch_loader, 2);

    }

$recordcount++;

    }

}

$query->finish();

if($recordcount > 0) {

        $consortium_authorities_batch_loader->autoflush(1);
        close($consortium_authorities_batch_loader, 2) || "cannot close $consortium_authorities_batch_loader: $!";
}

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$end_external_update_time = "$HOUR:$MIN:$SEC";

}

####END: CREATING FILES FOR LOAD INTO CONSORTIUM AUTHORITIES TABLES

$dbh->disconnect;

($HOUR, $MIN, $SEC) = (localtime)[2, 1, 0];
$end_time = "$HOUR:$MIN:$SEC";

print "Start time: " . $start_time . "\nEnd time: " . $end_time . "\n";

#SUBROUTINES

sub hashes_and_arrays {

$hash_files = $baseurl . '/hashfiles';

while (defined($next_hash_file = <$hash_files/*.pl>)) {
require $next_hash_file;
}

}

